package com.brspace.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.brspace.qualitycontrol.entity.*;
import com.brspace.qualitycontrol.mapper.*;
import com.brspace.qualitycontrol.service.ProcessNodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.utils.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-02
 */
@Service
public class ProcessNodeServiceImpl extends ServiceImpl<ProcessNodeMapper, ProcessNode> implements ProcessNodeService {
    @Resource
    private LeaveMapper leaveMapper;
    @Resource
    private MeetApplyMapper meetApplyMapper;
    @Resource
    private LetterMapper letterMapper;
    @Resource
    private PeopleHotlineMapper peopleHotlineMapper;
    @Resource
    private TownMatterMapper townMatterMapper;
    @Resource
    private IncomingCallMapper callMapper;
    /**
     * 根据id查询节点
     * @Params pId
     * @return null
     */
    @Override
    public List<ProcessNode> findByPid(int pId) {
        QueryWrapper<ProcessNode> queryWrapper = new QueryWrapper<>();
        return baseMapper.selectList(queryWrapper.eq("p_id", pId));
    }

    /**
     *@Description 保存节点
     *@Params [node]
     *@author Hugo
     *@date 2021-03-23 16:44
     *@return void
     **/
    @Override
    public void saveNode(ProcessNode node) {
        checkNode(node);
        baseMapper.insert(node);
    }
    /**
     * 获取路游序号
     * @Author: wangt
     * @Date: 2021-04-23 15:05:05
     */
    @Override
    public Object findSort(String id) {
        Leaveing leaveing = leaveMapper.selectById(id);
        return baseMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id",leaveing.getNodeId())).getSortNum();
    }

    @Override
    public Object findSortSanZhong(String id) {
        MeetApply meetApply = null;
        try {
            meetApply = meetApplyMapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SecurityException("流程已结束！");
        }
        return baseMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id",meetApply.getNodeId())).getSortNum();
    }

    @Override
    public Object findLetter(String id) {
        if ("undefined".equals(id) || id==null || "null".equals(id)){
            return 0;
        }
        Letter letter = letterMapper.selectById(id);
        if (letter.getNodeId()==0){
            throw new SecurityException("流程已结束！");
        }
        return baseMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id",letter.getNodeId())).getSortNum();
    }

    @Override
    public Object findPeoPle(String id) {
        PeopleHotline peopleHotline = peopleHotlineMapper.selectById(id);
        if (peopleHotline.getNodeId()==0){
            throw new SecurityException("流程已结束");
        }
        return baseMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id",peopleHotline.getNodeId())).getSortNum();
    }

    @Override
    public Object finTown(String id) {
        if ("undefined".equals(id) || id==null || "null".equals(id)){
            return 0;
        }
        TownMatter townMatter = townMatterMapper.selectById(id);
        if (townMatter.getNodeId()==0){
            throw new SecurityException("流程已结束！");
        }
        return baseMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id",townMatter.getNodeId())).getSortNum();
    }

    @Override
    public Object findCall(String id) {
        if ("undefined".equals(id) || id==null || "null".equals(id)){
            return 0;
        }
        IncomingCall call = callMapper.selectById(id);
        if (call.getNodeId()==0){
            throw new SecurityException("流程已结束！");
        }
        return baseMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id",call.getNodeId())).getSortNum();
    }

    public void checkNode(ProcessNode node){
        if ("".equals(node.getName())){
            throw new ServiceException("节点名称不能为空");
        }
        Map<String,Object> map = new HashMap<>(16);
        Map<String,Object> numberMap = new HashMap<>(16);
        map.put("p_id",node.getPId());
        map.put("name",node.getName());
        if (baseMapper.selectList(new QueryWrapper<ProcessNode>().allEq(map,false)).size() > 0){
            throw new ServiceException("节点重复");
        }
        numberMap.put("sort_num",node.getSortNum());
        numberMap.put("p_id",node.getPId());
        if (baseMapper.selectList(new QueryWrapper<ProcessNode>().allEq(numberMap,false)).size() > 0 ){
            throw new ServiceException("序列号重复");
        }
    }
}
